package com.leo.component;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.LiftComponent;
import com.almasb.fxgl.entity.component.Component;
import com.leo.CacheApp;
import com.leo.data.Rect;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.util.Duration;

/**
 * @Author: wangss
 * @CreateTime: 2023-06-17  22:06
 * @Description: TODO
 */
public class CacheComponent extends Component {
    @Override
    public void onAdded() {
        // 获取app中的rect1
        CacheApp cacheApp = (CacheApp) FXGL.getApp();
//        CacheApp appCast = FXGL.getAppCast();

        Rect rect1 = cacheApp.getRect1();

        Rect rect = entity.getObject("rect");
        entity.setPosition(rect.getX(), rect.getY());
        entity.getViewComponent().addChild(new Rectangle(rect.getWidth(), rect.getHeight(), Color.web(rect.getColor())));
        // 判断运动方向
        LiftComponent liftComponent = new LiftComponent();
        if ("hor".equals(rect.getMoveInfo().getDir())) {
            liftComponent.setGoingRight(true);
            liftComponent.xAxisDistanceDuration(rect.getMoveInfo().getDistance(), Duration.seconds(rect.getMoveInfo().getDuration()));
        } else {
            liftComponent.setGoingUp(true);
            liftComponent.yAxisDistanceDuration(rect.getMoveInfo().getDistance(), Duration.seconds(rect.getMoveInfo().getDuration()));
        }
        entity.addComponent(liftComponent);
    }
}
